#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>

#define SHM_SIZE 1024
void clear(int shmid,void * buf);
int main(){
	key_t key=ftok("./",45);
	if(key==-1){
		perror("ftok:");
		exit(1);
	}
	int shmid=shmget(key,SHM_SIZE,0666|IPC_CREAT);	
	if(shmid==-1){
		perror("shmget:");
		exit(1);
	}
	void *addr=shmat(shmid,NULL,0);
	if(addr==(void*)-1){
		perror("shmat");
		clear(shmid,addr);
		exit(1);
	}
	fgets(addr,SHM_SIZE,stdin);	
	fputs(addr,stdout);
	shmdt(addr);
	clear(shmid,addr);
	return 0;
}
#if 1
void clear(int shmid,void * buf){
	shmctl(shmid,IPC_RMID,buf);
}
#endif
